using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Bll;
using IGlobalist.Cms.Model;
using System.Collections.Generic;

namespace IGlobalist.Cms.Admin.Control
{
    public partial class PageList : AdminBaseUserControl
    {
        public event EventHandler EditPage;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public void DoBindPage(object sender, EventArgs e)
        {
            PageTree ucPageTree = (PageTree)sender;
            long pageID = Convert.ToInt64(ucPageTree.SelectedValue);
            IPage page = PageController.GetPage(pageID);

            //ClassList ucClassList = (ClassList)sender;
            //BindPage(Convert.ToInt64(ucClassList.SelectedValue));
        }

        public void BindPage(long classID)
        {
            ICmsClass clazz = ClassController.GetClass(classID);
            Session[SessionVariable.Clazz.CurrentID] = classID;
            gvPage.DataSource = PageController.GetPageListByClass(classID);
            gvPage.DataBind();
            ViewState["PageID"] = null;
        }

        public void DoDelete(object sender, EventArgs e)
        {
            if (ViewState["PageID"] != null) PageController.DeletePage(Convert.ToInt64(ViewState["PageID"]));
            BindPage(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
        }

        public void DoHidden(object sender, EventArgs e)
        {
            this.Visible = false;

        }

        public void DoShow(object sender, EventArgs e)
        {
            this.Visible = true;
            BindPage(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));

        }

        public string SelectedID
        {
            get { return Convert.ToString(ViewState["PageID"]); }
            set { ViewState["PageID"] = value; }
        }

        protected void gvPage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            long ID = Convert.ToInt64(e.CommandArgument);
            ViewState["PageID"] = ID;
            if (e.CommandName == "editPage")
            {
                if (EditPage != null)
                {
                    EditPage(this, new EventArgs());
                }  
            }

            if (e.CommandName == "delPage")
            {
                PageController.DeletePage(Convert.ToInt64(ID));
                BindPage(Convert.ToInt64(Session[SessionVariable.Clazz.CurrentID]));
            }
        }
    }
}